Trò chơi bắn đạn đơn giản sử dụng Unity
2 using System.Collections.Generic;
3 using UnityEngine;
4
5 public class PlayerCtrl : MonoBehaviour {
6
7 [Tooltip("This is a positive integer which speeds up the player movement")]
8 public int speedBoost; // set this to 5
9 public float jumpSpeed; // set this to 600
10 public bool isGrounded;
11 public Transform feet;
12 public float feetRadius;
13 public float boxWidth;
14 public float boxHeight;
15 public float delayForDoubleJump;
16 public LayerMask whatIsGround;
17 public Transform leftbulletSpawnPos;
18 public Transform rightbulletSpawnPos;
19 public GameObject leftbullet;
20 public GameObject rightbullet;
21 public bool leftPressed, rightPressed;
22
23 Rigidbody2D rb;
24 SpriteRenderer sr;
25 Animator anim;
26
27 bool isJumping, canDoubleJump;
28
29 // Use this for initialization
30 void Start () {
31 rb = GetComponent<Rigidbody2D>();
32 sr = GetComponent<SpriteRenderer>();
33 anim = GetComponent<Animator>();
34 }
35
36 // Update is called once per frame
37 void Update () {
38 isGrounded = Physics2D.OverlapBox(new Vector2(feet.position.x, feet.position.y), new Vector2(boxWidth, boxHeight), 360.0f, whatIsGround);
39
40 float playerSpeed = Input.GetAxisRaw("Horizontal"); // value will be 1, -1 or 0
41
42 playerSpeed *= speedBoost;
43
44 if (playerSpeed != 0)
45 MovePlayer(playerSpeed);
46 else
47 StopMoving();
48
49 if (Input.GetButtonDown("Jump"))
50 Jump();
51
52 if (Input.GetButtonDown("Fire1"))
53 {
54 FireBullets();
55 }
56
57 ShowFalling();
58
59 if (leftPressed)
60 MovePlayer(-speedBoost);
61
62 if (rightPressed)
63 MovePlayer(speedBoost);
64 }
65
66 private void OnDrawGizmos()
67 {
68 //Gizmos.DrawWireSphere(feet.position, feetRadius);
69
70 Gizmos.DrawWireCube(feet.position, new Vector3(boxWidth, boxHeight, 0));
71 }
72
73 void MovePlayer(float playerSpeed)
74 {
75 rb.velocity = new Vector2(playerSpeed, rb.velocity.y);
76
77 if (playerSpeed < 0)
78 sr.flipX = true;
79 else if (playerSpeed > 0)
80 sr.flipX = false;
81
82 if (!isJumping)
83 anim.SetInteger("State", 1);
84 }
85
86 void StopMoving()
87 {
88 rb.velocity = new Vector2(0, rb.velocity.y);
89
90 if (!isJumping)
91 anim.SetInteger("State", 0);
92 }
93
94 void ShowFalling()
95 {
96 if (rb.velocity.y < 0)
97 {
98 anim.SetInteger("State", 3);
99 }
100 }
101
102 void Jump()
103 {
104 if (isGrounded)
105 {
106 isJumping = true;
107 rb.AddForce(new Vector2(0, jumpSpeed)); // simply make the player jump in the y axis or upwards
108 anim.SetInteger("State", 2);
109
110 Invoke("EnableDoubleJump", delayForDoubleJump);
111 }
112
113 if (canDoubleJump && !isGrounded)
114 {
115 rb.velocity = Vector2.zero;
116 rb.AddForce(new Vector2(0, jumpSpeed)); // simply make the player jump in the y axis or upwards
117 anim.SetInteger("State", 2);
118
119 canDoubleJump = false;
120 }
121 }
122
123 void EnableDoubleJump()
124 {
125 canDoubleJump = true;
126 }
127
128 void FireBullets()
129 {
130 if (sr.flipX)
131 {
132 Instantiate(leftbullet, leftbulletSpawnPos.position, Quaternion.identity);
133 }
134
135 if (!sr.flipX) {
136 Instantiate(rightbullet, rightbulletSpawnPos.position, Quaternion.identity);
137 }
138 }
139
140 public void MobileMoveLeft()
141 {
142 leftPressed = true;
143 }
144
145 public void MobileMoveRight()
146 {
147 rightPressed = true;
148 }
149
150 public void MobileStop()
151 {
152 leftPressed = false;
153 rightPressed = false;
154
155 StopMoving();
156 }
157
158 public void MobileFireBullets()
159 {
160 FireBullets();
161 }
162
163 public void MobileJump()
164 {
165 Jump();
166 }
167
168 void OnCollisionEnter2D(Collision2D other)
169 {
170 if (other.gameObject.CompareTag("Ground"))
171 {
172 isJumping = false;
173 }
174
175 if (other.gameObject.CompareTag("Enemy"))
176 {
177 Destroy(gameObject);
178 }
179 }
180 }
public int speedBoost; set this to 5
public float jumpSpeed; set this to 600
Use this for initialization
Update is called once per frame
float playerSpeed = Input.GetAxisRaw("Horizontal"); value will be 1, -1 or 0
Gizmos.DrawWireSphere(feet.position, feetRadius);
rb.AddForce(new Vector2(0, jumpSpeed)); simply make the player jump in the y axis or upwards
rb.AddForce(new Vector2(0, jumpSpeed)); simply make the player jump in the y axis or upwards